import MD5 from "../../core/utils/md5";
import { clamp, keepDecimal, ObjectDelKey, ObjectMapCb, randomChoice, createID, ArrUnique, copyObj, getRandomNum, getAttrOrOther } from "../../core/utils/utils";
import { attrDefine } from "../defines/attr_define";
import { MoneyDefine } from "../defines/money_define";
import { LimbsTypeDefine } from "../defines/people_limbs";
import { presitgeDefine } from "../defines/presitge_define";
import { RaceDefine } from "../defines/race_define";

export class PeopleAttr{
	constructor(data = {}){
		// 种族
		this.race = data.race || RaceDefine.human.key;
		// 名称
		this.name = data.name || "神秘人物";
		// 性别
		this.gender = data.gender || randomChoice(["male", "female"]);

		this._id = data._id || MD5(new Date().getTime() + this.name + (Math.random() * 10000));

		// 功法
		this.methodForce = data.methodForce || {};
		this.methodXian = data.methodXian || {};

		// 关于修仙连体的进度
		this.forceNum = data.forceNum || 0;
		this.xianNum = data.xianNum || 0;

		// 技能，这里仅存拥有的技能
		// 如果没有技能，那么将会获取种族自带技能
		// this.skills = ArrUnique([...RaceDefine[this.race].skills, (data.skills || [])]);
		this.skills = (data.skills || []).length <= 0 ? [...RaceDefine[this.race].skills] : data.skills;
		// 这里存放用过的技能的熟练度
		this.skillsNum = {};

		// 声望
		this.presitge = data.presitge || {};
		// 好感，仅为npc对玩家的作用
		this.favor = data.favor || 0;
		// 天赋
		this.talents = data.talents || [];
		// 身份
		this.identitys = data.identitys || [];
		// 副业
		this.sideHustle = data.sideHustle || [];

		// 世界定位
		this.worldPos = data.worldPos || [0, 0];
		// 地图定位
		this.mapPos = data.mapPos || [0, 0];

		// 铜钱
		this[MoneyDefine.money.key] = data[MoneyDefine.money.key] || 0;
		// 灵石
		this[MoneyDefine.stone.key] = data[MoneyDefine.stone.key] || 0;

		// 修炼资质，也就是修炼速度
		this.practiceDiff = data.diff
			|| getRandomNum() > .1 ? clamp(getRandomNum() * 2, 0.5, 2) : clamp(getRandomNum() * 5, 2, 5);
		// 生灵之间的属性差异，这个差异极小，只为了不让所有生灵的属性一致
		this.attrDiff = data.attrDiff || (1 + getRandomNum() * .4 - .2);

		let raceAttr = RaceDefine[this.race].attr;

		// 基本属性
		this.basicAttr = {
			// 气血（生灵的身体强度）
			[attrDefine.blood.key]: getAttrOrOther(data.basicAttr, attrDefine.blood.key, raceAttr[attrDefine.blood.key], 10),
			// 灵力（生灵的丹田强度）
			[attrDefine.spirit.key]: getAttrOrOther(data.basicAttr, attrDefine.spirit.key, raceAttr[attrDefine.spirit.key], 0),
			// 神识（生灵的神府强度）
			[attrDefine.cons.key]: getAttrOrOther(data.basicAttr, attrDefine.cons.key, raceAttr[attrDefine.cons.key], 10),
			// 敏捷（生灵的反应能力）
			[attrDefine.agile.key]: getAttrOrOther(data.basicAttr, attrDefine.agile.key, raceAttr[attrDefine.agile.key], 6),
			// 年龄上限
			[attrDefine.ageMax.key]: getAttrOrOther(data.basicAttr, attrDefine.ageMax.key, raceAttr[attrDefine.ageMax.key], 50),

			// 气血攻击力
			[attrDefine.bloodAtk.key]: getAttrOrOther(data.basicAttr, attrDefine.bloodAtk.key, raceAttr[attrDefine.bloodAtk.key], 1),
			// 灵力攻击力
			[attrDefine.spiritAtk.key]: getAttrOrOther(data.basicAttr, attrDefine.spiritAtk.key, raceAttr[attrDefine.spiritAtk.key], 0),
			// 神识攻击力
			[attrDefine.consAtk.key]: getAttrOrOther(data.basicAttr, attrDefine.consAtk.key, raceAttr[attrDefine.consAtk.key], 0),
			// 物理防御力
			[attrDefine.bloodDef.key]: getAttrOrOther(data.basicAttr, attrDefine.bloodDef.key, raceAttr[attrDefine.bloodDef.key], 1),
			// 灵力防御力
			[attrDefine.spiritDef.key]: getAttrOrOther(data.basicAttr, attrDefine.spiritDef.key, raceAttr[attrDefine.spiritDef.key], 1),
			// 神识防御力
			[attrDefine.consDef.key]: getAttrOrOther(data.basicAttr, attrDefine.consDef.key, raceAttr[attrDefine.consDef.key], 1),
			// 闪避
			[attrDefine.dodge.key]: getAttrOrOther(data.basicAttr, attrDefine.dodge.key, raceAttr[attrDefine.dodge.key], 1),
			// 暴击
			[attrDefine.crit.key]: getAttrOrOther(data.basicAttr, attrDefine.crit.key, raceAttr[attrDefine.crit.key], 1),
			// 幸运
			[attrDefine.lucky.key]: getAttrOrOther(data.basicAttr, attrDefine.lucky.key, raceAttr[attrDefine.lucky.key], 1),
			// 暴击伤害
			[attrDefine.critDamage.key]: getAttrOrOther(data.basicAttr, attrDefine.critDamage.key, raceAttr[attrDefine.critDamage.key], 2),

			// 属性攻击
			[attrDefine.woodAtk.key]: getAttrOrOther(data.basicAttr, attrDefine.woodAtk.key, raceAttr[attrDefine.woodAtk.key], 0),
			[attrDefine.waterAtk.key]: getAttrOrOther(data.basicAttr, attrDefine.waterAtk.key, raceAttr[attrDefine.waterAtk.key], 0),
			[attrDefine.fireAtk.key]: getAttrOrOther(data.basicAttr, attrDefine.fireAtk.key, raceAttr[attrDefine.fireAtk.key], 0),
			[attrDefine.earthAtk.key]: getAttrOrOther(data.basicAttr, attrDefine.earthAtk.key, raceAttr[attrDefine.earthAtk.key], 0),
			[attrDefine.goldAtk.key]: getAttrOrOther(data.basicAttr, attrDefine.goldAtk.key, raceAttr[attrDefine.goldAtk.key], 0),
			// 属性防御
			[attrDefine.woodDef.key]: getAttrOrOther(data.basicAttr, attrDefine.woodDef.key, raceAttr[attrDefine.woodDef.key], 0),
			[attrDefine.waterDef.key]: getAttrOrOther(data.basicAttr, attrDefine.waterDef.key, raceAttr[attrDefine.waterDef.key], 0),
			[attrDefine.fireDef.key]: getAttrOrOther(data.basicAttr, attrDefine.fireDef.key, raceAttr[attrDefine.fireDef.key], 0),
			[attrDefine.earthDef.key]: getAttrOrOther(data.basicAttr, attrDefine.earthDef.key, raceAttr[attrDefine.earthDef.key], 0),
			[attrDefine.goldDef.key]: getAttrOrOther(data.basicAttr, attrDefine.goldDef.key, raceAttr[attrDefine.goldDef.key], 0),
		};

		// 肢体
		this.limbs = this.getLimbs(data);

		// 固有属性，不会因为刷新等原因重置为最大值
		this.useAttr = {};
		
		// 只赋予一次，不会因为转换数据时导致满血
		this.useAttr[attrDefine.hp.key] = (data.useAttr && data.useAttr[attrDefine.hp.key]) || this.attr[attrDefine.hpMax.key];
		this.useAttr[attrDefine.mana.key] = (data.useAttr && data.useAttr[attrDefine.mana.key]) || this.attr[attrDefine.manaMax.key];
		this.useAttr[attrDefine.age.key] = (data.useAttr && data.useAttr[attrDefine.age.key]) || ~~(this.attr[attrDefine.ageMax.key] * .2);
	}

	getAttr(){
		let race = RaceDefine[this.race];
		let attr = copyObj(this.basicAttr);
		
		// 默认的转换
    // 攻击力
		attr[attrDefine.bloodAtk.key] = this.basicAttr[attrDefine.blood.key] * .7;
		attr[attrDefine.spiritAtk.key] = this.basicAttr[attrDefine.spirit.key] * .6;
		attr[attrDefine.consAtk.key] = this.basicAttr[attrDefine.cons.key] * .3;
		// 防御力
		attr[attrDefine.bloodDef.key] = this.basicAttr[attrDefine.blood.key] * .3;
		attr[attrDefine.spiritDef.key] = this.basicAttr[attrDefine.spirit.key] * .3;
		attr[attrDefine.consDef.key] = this.basicAttr[attrDefine.cons.key] * .3;
		// 生命值
		attr[attrDefine.hpMax.key] = this.basicAttr[attrDefine.blood.key] * 5;
		// 灵气
		attr[attrDefine.manaMax.key] = this.basicAttr[attrDefine.spirit.key] * 10;
		// 攻击速度
		attr[attrDefine.atkSpeed.key] = (this.basicAttr[attrDefine.agile.key] + (this.basicAttr[attrDefine.cons.key] / 10)) * .5;
		// 暴击率
		attr[attrDefine.crit.key] = this.basicAttr[attrDefine.crit.key] + this.basicAttr[attrDefine.lucky.key] / 2;
		// 暴击伤害
		attr[attrDefine.critDamage.key] = this.basicAttr[attrDefine.critDamage.key];
		// 闪避比较值，双方单位进行比较，根据一定的算法来计算几率
		attr[attrDefine.dodge.key] = this.basicAttr[attrDefine.dodge.key] + this.basicAttr[attrDefine.lucky.key] / 2;

		// 种族对属性的影响
		if(race && race.transform){
			race.transform(this.basicAttr, attr);
		}
		// TODO 在这里填写功法对属性的修改

		// 给基本属性添加基本值
		['blood', 'spirit', 'cons', 'wood', 'water', 'fire', 'earth', 'gold'].map(key => {
			attr[key + 'Atk'] += this.basicAttr[key + 'Atk'] || 0;
			attr[key + 'Def'] += this.basicAttr[key + 'Def'] || 0;
		})

    attr = ObjectMapCb(attr, num => {
      return keepDecimal(num * this.attrDiff, 0)
    })

		return attr;
	}

	getLimbs(data){
		return (data.limbs || RaceDefine[this.race].limbs).map(item => {
			let race = RaceDefine[this.race].limbs.find(limb => limb.key == item.key) || {};

			return {
				// 状态，当为0的时候这个肢体就为残疾无法使用
				// status: 1,
				// 血量占比
				// hp: 1,
				// 力量占比，在使用这个肢体力量的振幅或衰减
				power: 1,
				// 是否致命
				// dead: false,
				// 可装备数
				equipNum: 0,
				// 所有装备
				equips: [],
				text: LimbsTypeDefine[item.key].text,
				...race,
				...item,
			}
		}).filter(i => i);
	}

	get attr(){
		return this.getAttr();
	}
}